package ch.subgate.pongwars;

import android.content.Context;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

public class GameThread extends Thread {

	/*
	 * ---- Thread related members ----
	 */

    /** SurfaceHolder gives access to the canvas */
    private final SurfaceHolder mSurfaceHolder;

    /** Variable to stop the threads execution */
    private boolean mIsRunning;

	/*
	 * ---- Metrics ----
	 */

    /** GameContext object is holding all the necessary metric data */
    private GameContext mGameContext;

	/*
	 * ---- Rendering objects ----
	 */

    /** Renderer is an object that centralizes draw() calls */
    private Renderer mRenderer;

    /** GameThread constructor */
    public GameThread(SurfaceHolder holder, Context context) {
        mSurfaceHolder = holder;

        // Initialize thread state
        mIsRunning = false;

        // Create renderer
        mRenderer = new Renderer(context);
    } // Constructor

    @Override
    public void run() {
        Canvas canvas;
        while (mIsRunning) {

            // Update game
            mGameContext.update();

            // Draw game
            canvas = null;
            try {
                canvas = mSurfaceHolder.lockCanvas();
                synchronized (mSurfaceHolder) {
                    mRenderer.draw(canvas, mGameContext);
                } // synchronized
            } // try
            catch (Exception e) {
                // do nothing about it
            } // catch
            finally {
                if (canvas != null) {
                    mSurfaceHolder.unlockCanvasAndPost(canvas);
                } // if
            } // finally
        } // while

        if (mGameContext instanceof GameContextP2P) { mGameContext.release(); }
    } // run

    public void setSurfaceSize(int width, int height) {
        synchronized (mSurfaceHolder) {
            mGameContext.initMetrics(width, height);
            mRenderer.init(mGameContext);
        } // synchronized
    } // setSurfaceSize

    public void setRunning(boolean running) {
        mIsRunning = running;
    } // setRunning

    public boolean handleTouchEvent(MotionEvent e) {
        int action = e.getActionMasked();
        for (int i = 0; i < e.getPointerCount(); i++) {
            float diX = GameHelper.getDICXFromSC(mGameContext, e.getX(i));
            float diY = GameHelper.getDICYFromSC(mGameContext, e.getY(i));
            mGameContext.handleInput(action, diX, diY);
        } // for
        return true;
    } // handleTouchEvent

    public void setGameMode(Context context, int mode, float paddleSize, boolean isServer, float volume) {
        switch (mode) {
            case Constants.MODE_PVPN:
                mGameContext = new GameContextP2P(context, volume);
                break;
            case Constants.MODE_PVP:
                mGameContext = new GameContextMP(context, volume);
                break;
            default:
                mGameContext = new GameContextSP(context, volume);
        } // switch
        mGameContext.initGameObjects(paddleSize, isServer);
    } // setGameMode

    public GameContext getGameContext() { return mGameContext; }

} // GameThread